<?php

namespace App\Http\Controllers\Admin;

use App\Model\Role;
use App\Model\Permission;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;

class RoleController extends Controller
{
    /**
     * 角色列表显示页面
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //分页、多条件查询
        $role = Role::orderBy('id','asc')
            ->where(function($query) use($request){
                $rolename = $request->input('rolename');
                if (!empty($rolename)) {
                    # code...
                    $query->where('role_name','like','%'.$rolename.'%');
                }
            })->paginate($request->input('num')?$request->input('num'):5);

        $i = 1;

        return view('admin.role.list',compact('role','request','i'));
    }

    /**
     * 角色创建页面
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('admin.role.create');
    }

    /**
     * 接收表单页面，创建角色
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //接收表单数据
        $info = $request->all();
        // dd($info);

        //执行创建操纵，存储到数据库
        $res = Role::insert($info,true);
        //判断执行结果，将状态返回至数据库
        if ($res) {
            # code...
            $data = [
                'status'=>0,
                'message'=>'添加成功'
            ];
        } else {
            $data = [
                'status'=>1,
                'message'=>'添加失败'
            ];
        }
        return $data;
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * 角色信息修改页面
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $role = Role::find($id);
        return view('admin.role.edit',compact('role'));
    }

    /**
     * 接收表单数据，更新角色信息
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $info = $request->all();
        //更新当前管理员记录
        $res = Role::where('id', $id)
            ->update($info,true);
        //判断执行结果，将状态返回至页面
        if ($res) {
            # code...
            $data = [
                'status' => 0,
                'message' => '修改成功'
            ];
        }else{
            $data = [
                'status' => 1,
                'message' => '修改失败'
            ];
        }
        return $data;
    }

    /**
     * 删除角色
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //根据角色ID，删除角色信息
        $res = Role::where('id', $id)
            ->delete();
        //判断执行结果，将状态返回至页面
        if ($res) {
            # code...
            $data = [
                'status' => 0,
                'message' => '删除成功'
            ];
        }else{
            $data = [
                'status' => 1,
                'message' => '删除失败'
            ];
        }
        return $data;
    }

    /**
     * 角色授权页面
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function auth($id)
    {
    	//根据ID，查询角色数据
        $role = Role::find($id);
    
    	//查询所有权限
        $permission = Permission::get();

        //查询角色所拥有的权限，并遍历赋值给data
        $info = $role->permission;
        $data = [];
        foreach ($info as $k => $v) {
        	# code...
        	$data[] = $v->id;
        }

    	return view('admin.role.auth',compact('role','permission','data'));
    }

    /**
     * 接收表单数据，更新角色权限
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function doAuth(Request $request)
    {
    	//接收表单数据
        $info = $request->all();
    	// dd($info);

    	//删除角色原有权限
        DB::table('role_permission')->where('role_id',$info['id'])->delete();

    	//判断新赋予权限是否为空
        if (!empty($info['permission_id'])) {
    		//遍历循环权限，并存储至数据库
    		foreach ($info['permission_id'] as $k => $v) {
	    		# code...
	    		$res = DB::table('role_permission')->insert(['role_id'=>$info['id'],'permission_id'=>$v]);
	    	}
    	}else{
    		$res = 1;
    	}

        //判断执行结果，将状态返回至页面
    	if ($res) {
            # code...
            $data = [
                'status' => 0,
                'message' => '授权成功'
            ];
        }else{
            $data = [
                'status' => 1,
                'message' => '授权失败'
            ];
        }
        return $data;
    }
}
